import {
  MigrationInterface,
  QueryRunner,
  Table,
  TableForeignKey,
} from 'typeorm';

export class CreateWorksTable1730620000000 implements MigrationInterface {
  public async up(queryRunner: QueryRunner): Promise<void> {
    // 创建作品表
    await queryRunner.createTable(
      new Table({
        name: 'works',
        columns: [
          {
            name: 'id',
            type: 'int',
            isPrimary: true,
            isGenerated: true,
            generationStrategy: 'increment',
          },
          {
            name: 'title',
            type: 'varchar',
            length: '200',
            comment: '作品标题',
          },
          {
            name: 'description',
            type: 'text',
            comment: '作品描述',
          },
          {
            name: 'category',
            type: 'enum',
            enum: ['food', 'health', 'travel'],
            comment: '作品类别',
          },
          {
            name: 'status',
            type: 'enum',
            enum: ['draft', 'published', 'archived'],
            default: "'draft'",
            comment: '作品状态',
          },
          {
            name: 'isPublic',
            type: 'boolean',
            default: true,
            comment: '是否公开',
          },
          {
            name: 'mediaType',
            type: 'enum',
            enum: ['image', 'video'],
            comment: '媒体类型',
          },
          {
            name: 'imagesUrls',
            type: 'text',
            isNullable: true,
            comment: '图片URL列表（JSON数组）',
          },
          {
            name: 'videoUrl',
            type: 'varchar',
            length: '500',
            isNullable: true,
            comment: '视频URL',
          },
          {
            name: 'videoCover',
            type: 'varchar',
            length: '500',
            isNullable: true,
            comment: '视频封面URL',
          },
          {
            name: 'tags',
            type: 'text',
            isNullable: true,
            comment: '作品标签（JSON数组）',
          },
          {
            name: 'viewsCount',
            type: 'int',
            default: 0,
            comment: '浏览量',
          },
          {
            name: 'likesCount',
            type: 'int',
            default: 0,
            comment: '点赞数',
          },
          {
            name: 'collectionsCount',
            type: 'int',
            default: 0,
            comment: '收藏数',
          },
          {
            name: 'commentsCount',
            type: 'int',
            default: 0,
            comment: '评论数',
          },
          {
            name: 'sharesCount',
            type: 'int',
            default: 0,
            comment: '分享数',
          },
          {
            name: 'authorId',
            type: 'int',
            comment: '作者ID',
          },
          {
            name: 'location',
            type: 'varchar',
            length: '200',
            isNullable: true,
            comment: '地点名称',
          },
          {
            name: 'latitude',
            type: 'decimal',
            precision: 10,
            scale: 7,
            isNullable: true,
            comment: '纬度',
          },
          {
            name: 'longitude',
            type: 'decimal',
            precision: 10,
            scale: 7,
            isNullable: true,
            comment: '经度',
          },
          {
            name: 'publishedAt',
            type: 'timestamp',
            isNullable: true,
            comment: '发布时间',
          },
          {
            name: 'createdAt',
            type: 'timestamp',
            default: 'CURRENT_TIMESTAMP',
            comment: '创建时间',
          },
          {
            name: 'updatedAt',
            type: 'timestamp',
            default: 'CURRENT_TIMESTAMP',
            onUpdate: 'CURRENT_TIMESTAMP',
            comment: '更新时间',
          },
        ],
      }),
      true,
    );

    // 添加外键约束
    await queryRunner.createForeignKey(
      'works',
      new TableForeignKey({
        columnNames: ['authorId'],
        referencedColumnNames: ['id'],
        referencedTableName: 'users',
        onDelete: 'CASCADE',
      }),
    );

    // 创建索引
    await queryRunner.query(
      'CREATE INDEX `IDX_works_category` ON `works` (`category`)',
    );
    await queryRunner.query(
      'CREATE INDEX `IDX_works_status` ON `works` (`status`)',
    );
    await queryRunner.query(
      'CREATE INDEX `IDX_works_authorId` ON `works` (`authorId`)',
    );
    await queryRunner.query(
      'CREATE INDEX `IDX_works_publishedAt` ON `works` (`publishedAt`)',
    );
  }

  public async down(queryRunner: QueryRunner): Promise<void> {
    // 删除索引
    await queryRunner.query('DROP INDEX `IDX_works_publishedAt` ON `works`');
    await queryRunner.query('DROP INDEX `IDX_works_authorId` ON `works`');
    await queryRunner.query('DROP INDEX `IDX_works_status` ON `works`');
    await queryRunner.query('DROP INDEX `IDX_works_category` ON `works`');

    // 删除表
    await queryRunner.dropTable('works');
  }
}
